package arrayandstring;
/**
 * https://leetcode-cn.com/problems/maximum-number-of-words-found-in-sentences/
 */
public class E2114_MaximumNumberofWordsFoundinSentences {
    /**
     * 时间复杂度 O(n) 空间复杂度 O(n) 
     */
    public int mostWordsFound(String[] sentences) {
        int max = 0;
        for (int i = 0; i < sentences.length; ++i) {
            String[] str = sentences[i].split(" ");
            max = max > str.length ? max : str.length;
        }
        return max;
    }
    /**
     * (java不推荐)官方解法思路
     * 时间复杂度 O(∑ni) 空间复杂度 O(n) [ni - sentences[i].length]
     * 统计空格的个数
     * 空格个数+1就是句子中单词的数量
     */
    public int mostWordsFound1(String[] sentences) {
        int max = 0;
        for (String sentence : sentences) {
            int count = 1;
            for (int i = 0; i < sentence.length(); i++) {
                if (sentence.charAt(i) == ' ') {
                    count++;
                }
            }
            max = count > max ? count : max;
        }
        return max;
    }
}
